<template>
	<view class="product">
		<view class="center">
			<view class="pro_switch">
				<u-subsection :list="list" mode="subsection" :current="current" @change="sectionChange" fontSize="25rpx"
					activeColor="#e14254"></u-subsection>
			</view>

			<!-- 搜索 -->
			<view class="search">
				<u-search v-model='search' searchIconSize="35" inputAlign="center" placeholder='请输入渠道进行查询'
					:showAction="false" height="50" bgColor="white" :clearabled="true"
					@search="handleSearch"></u-search>
			</view>
			<!-- 列表1 -->
			<view class="u-page">
				<u-list @scrolltolower="scrolltolower">
					<u-list-item v-for="(item, index) in indexList" :key="index">
						<view class="list_box">
							<view class="list_one">
								<span><b>{{item.productName}}</b></span>
								<!-- <span>2023-08-31 14:36:47</span> -->
							</view>
							<view class="list_two">
								<span class="list_two_sapn"><b>渠道编码</b>：{{item.channelCode}}</span>
								<span class="list_two_sapn"><b>渠道</b>：{{item.channel}}</span>
								<span class="list_two_sapn"><b>运营商</b>：{{item.operator}}</span>
							</view>

							<view class="list_three">
								<button v-if="current=='0'" plain="true" class="btn2" @click="down">下架</button>
								<button v-if="current=='1'" plain="true" class="btn3" @click="up">上架</button>
								<!-- <button plain="true" class="btn1" >切换</button> -->
							</view>
							<u-line></u-line>
							<span class="rules" @click="open(item)">详细规则 ></span>
						</view>
					</u-list-item>
				</u-list>
			</view>
		</view>
		<!-- 版心结束 -->
		<!-- 加载动画 -->
		<view>
			<u-toast ref="uToast"></u-toast>
		</view>
		<!-- 加在动画结束 -->
		<!-- 详细规则的弹出框 -->
		<view class="modal">
			<u-modal :show="show" @confirm="confirm" :title="title" :content='content'></u-modal>
		</view>
		<!-- 详细规则结束 -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: ['上架产品', '下架产品'],
				current: 0,
				newState: 1, //顶部tab切换
				title: '标题',
				content: '佣金规则是：10元',
				search: '', //搜索v-model
				// 列表
				indexList: [],
				pageNum: 1,
				pageSize: 10,
				total: 100, // 替换为实际的总数据量
				loading: false, // 控制加载动画的变量
				urls: [
					'https://cdn.uviewui.com/uview/album/1.jpg',
					'https://cdn.uviewui.com/uview/album/2.jpg',
					'https://cdn.uviewui.com/uview/album/3.jpg',
					'https://cdn.uviewui.com/uview/album/4.jpg',
					'https://cdn.uviewui.com/uview/album/5.jpg',
					'https://cdn.uviewui.com/uview/album/6.jpg',
					'https://cdn.uviewui.com/uview/album/7.jpg',
					'https://cdn.uviewui.com/uview/album/8.jpg',
					'https://cdn.uviewui.com/uview/album/9.jpg',
					'https://cdn.uviewui.com/uview/album/10.jpg',
				],
				// 加在动画
				show: false,
				loadlist: [{
					type: 'loading',
					title: '正在加载',
					message: "正在加载",
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/loading.png'
				}, ]
			}
		},
		onLoad() {
			this.loadmore()
			this.sectionChange(0);
		},
		computed: {
			getIcon() {
				return path => {
					return 'https://cdn.uviewui.com/uview/example/' + path + '.png';
				}
			},
		},
		methods: {
			// 顶部商品产品切换
			sectionChange(index) {
				this.current = index;
				this.pageNum = 1;
				const channel = this.search
				const stateMapping = {
					'上架': 1,
					'下架': 0
				};
				// console.log(this.current,'index产品情况')
				this.showToast(this.loadlist[0])
				this.indexList = []
				// 这里current是0是上架tab 1是下架tab
				const currentState = this.current == '0' ? '上架' : '下架';
				console.log(currentState, 'currentState');

				uni.request({
					url: 'http://101.126.42.213:9090/product/page',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						state: currentState,
						channel: channel,
					},
					method: "GET",
					success: (res) => {
						console.log(res, '请求的数据');
						this.loading = false; // 隐藏加载动画
						this.pageNum = res.data.current;
						this.pageSize = res.data.size;
						this.total = res.data.total;
						const newData = res.data.records;
						console.log(newData, 'newData');
						const filteredData = newData.filter(data => data.state == stateMapping[currentState]);
						this.indexList = this.indexList.concat(filteredData);
						console.log(this.indexList, 'indexList');
						// 更新pageNum
						this.pageNum++;
						// 如果还有更多数据，继续加载

					},
					fail: (err) => {
						this.loading = false; // 隐藏加载动画
						console.log("res faile", err);
					}
				});
			},


			// 列表加载事件
			scrolltolower() {
				if (this.hasMoreData()) {
					this.showToast(this.loadlist[0]);
					// 模拟异步请求，实际中可以替换为你的异步请求逻辑
					const currentState = this.current == '0' ? '上架' : '下架';
					const channel = this.search
					setTimeout(() => {
						this.loadmore(currentState, channel);
					}, 500);
				} else {
					// 没有更多数据时弹出提示
					this.showToast({
						type: 'text',
						message: '没有更多数据了',
						duration: 2000
					});
					console.log("No more data");
				}

			},
			hasMoreData() {
				// 判断是否还有更多数据需要加载
				return this.pageNum * this.pageSize < this.total; // 替换为实际的总数据量
			},
			loadmore(state, channel) {
				// console.log('state',state)
				this.loading = true; // 显示加载动画
				const stateMapping = {
					'上架': 1,
					'下架': 0
				};
				const currentState = this.current == '0' ? '上架' : '下架';
				console.log(currentState, 'currentState');
				uni.request({
					url: 'http://101.126.42.213:9090/product/page',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						state: currentState,
						channel: channel

					},
					method: "GET",
					success: (res) => {
						console.log(res, '请求的数据');
						this.loading = false; // 隐藏加载动画
						this.pageNum = res.data.current;
						this.pageSize = res.data.size;
						this.total = res.data.total;
						const newData = res.data.records;
						console.log(newData, 'newData');

						const filteredData = newData.filter(data => data.state == stateMapping[currentState]);
						this.indexList = this.indexList.concat(filteredData);
						console.log(this.indexList, 'indexList');
						// 更新pageNum
						this.pageNum++;
						// 如果还有更多数据，继续加载

					},
					fail: (err) => {
						this.loading = false; // 隐藏加载动画
						console.log("res faile", err);
					}
				});
			},

			// 加载动画
			showToast(params) {
				this.$refs.uToast.show({
					...params,
				})
			},
			// 详细规则弹出框
			open(item) {
				this.show = true
				if (item.commission == null) {
					this.$set(item, 'commission', 0);
				}
				this.content = this.content.replace('10', item.commission)
			},
			confirm() {
				this.show = false
			},
			handleSearch() {
				console.log(this.search, '触发搜索内容');
				const channel = this.search;
				this.pageNum = 1;
				this.loading = true; // 显示加载动画
				const stateMapping = {
					'上架': 1,
					'下架': 0
				};
				const currentState = this.current == '0' ? '上架' : '下架';
				console.log(currentState, 'currentState');
				uni.request({
					url: 'http://101.126.42.213:9090/product/page',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						state: currentState,
						channel: channel,
					},
					method: "GET",
					success: (res) => {
						console.log(res, '查询之后接口数据');
						this.loading = false; // 隐藏加载动画
						this.pageNum = res.data.current;
						this.pageSize = res.data.size;
						this.total = res.data.total;
						const newData = res.data.records;
						console.log(newData, 'newData');

						const filteredData = newData.filter(data => data.state == stateMapping[currentState]);
						this.indexList = filteredData;
						console.log(this.indexList, 'indexList');
						// 更新pageNum
						this.pageNum++;


					},
					fail: (err) => {
						this.loading = false; // 隐藏加载动画
						console.log("res faile", err);
					}
				});



			},
			// 下架操作
			down() {
				uni.request({
					url: 'http://101.126.42.213:9090/product/alterState',
					method: 'GET',
					data: {
						productCode: productCode
					},
					success: (res) => {
						this.showToast({
							type: 'text',
							message: '下架成功',
							duration: 2000
						});
					},
				})
			},
			// 上架操作
			up() {
				uni.request({
					url: "http://101.126.42.213:9090/product/alterState",
					method: 'GET',
					data: {
						productCode: productCode
					},
					success: (res) => {
						this.showToast({
							type: 'text',
							message: '上架成功',
							duration: 2000
						});
					}
				})
			},
		}
	}
</script>

<style src='./product.css' scoped>
</style>